package com.tdf.webblog.service.serviceImpl;

import com.tdf.webblog.dao.AdminRoleDAO;
import com.tdf.webblog.entity.AdminRole;
import com.tdf.webblog.entity.AdminUserRole;
import com.tdf.webblog.service.AdminRoleService;
import com.tdf.webblog.service.AdminUserRoleService;
import com.tdf.webblog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class AdminRoleServiceImpl implements AdminRoleService {

    @Autowired
    private UserService userService;

    @Autowired
    private AdminUserRoleService adminUserRoleService;

    @Autowired
    private AdminRoleDAO adminRoleDAO;

    @Override
    public List<AdminRole> listRolesByUser(String username) {
        int uid = userService.findByUsername(username).getId();
        List<Integer> rids = adminUserRoleService.listAllByUid(uid)
                .stream().map(AdminUserRole::getRid).collect(Collectors.toList());
        return adminRoleDAO.findAllById(rids);
    }
}
